package com.yst.gateway.monitor.controllers;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.groups.Default;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.yst.gateway.monitor.common.beans.Response;
import com.yst.gateway.monitor.common.validator.groups.Groups;
import com.yst.gateway.monitor.models.QzBusiness;
import com.yst.gateway.monitor.models.QzDictionary;
import com.yst.gateway.monitor.services.QzDictionaryService;

/**
 * 
 * 前置系统字典管理业务控制类
 * 
 * @author Administrator
 *
 */
@RestController
@RequestMapping(value = "/sys/dic")
public class QzDictionaryController {
	
	
	@Autowired
	private QzDictionaryService qzDictionaryService;
	
	
	@RequestMapping(value = "/add" , method = RequestMethod.POST)
	public Response add(@RequestBody @Validated({Default.class,Groups.Add.class}) QzDictionary qzDictionary) {
		Response response = new Response();
		if (qzDictionaryService.add(qzDictionary)) {
			response.success();
		} else {
			response.failure();
		}
		return response;
	}

	
	@RequestMapping(value = "/addByPid" , method = RequestMethod.POST)
	public Response addByPid(@RequestBody @Validated({Default.class,Groups.Add.class}) QzDictionary qzDictionary) {
		Response response = new Response();
		if (qzDictionaryService.insertByPid(qzDictionary)) {
			response.success();
		} else {
			response.failure();
		}
		return response;
	}
	
	
	/**
	 * 根据父级编码查询所有子集
	 * @param code
	 * @return
	 */
	@RequestMapping(value = "/getByGroup/{code}" ,method = RequestMethod.GET)
	public Response getByCode(@PathVariable("code") String code) {
		Response response = new Response();
		response.success(qzDictionaryService.getByCode(code));
		return response;
	}

	/**
	 * 根据pid 查询所有子集
	 * @param code
	 * @return
	 */
	@RequestMapping(value = "/getByPid/{id}" ,method = RequestMethod.GET)
	public Response getByPid(@PathVariable("id") String id) {
		Response response = new Response();
		response.success(qzDictionaryService.getByPid(id));
		return response;
	}
	
	
	
	@RequestMapping(value = "/checkName", method = RequestMethod.GET)
	public boolean checkName(@RequestParam String name) {
		List<QzDictionary> list = qzDictionaryService.selectByName(name);
		if (list == null || list.size() == 0) {
			return true;
		} 
		return false;
	}
	
	
	@RequestMapping(value = "/checkCode", method = RequestMethod.GET)
	public boolean checkCode(@RequestParam String code) {
		List<QzDictionary> list = qzDictionaryService.selectByCode(code);
		if (list == null || list.size() == 0) {
			return true;
		} 
		return false;
	}
	
	
	@RequestMapping(value = "/checkDicCode", method = RequestMethod.GET)
	public boolean checkDicCode(HttpServletRequest request) {
		String pid = request.getParameter("id");
		String [] codes = request.getParameterValues("code");
		String editCode = codes[1];
		List<QzDictionary> list = qzDictionaryService.selectByCodeAndPid(editCode, pid);
		if (list == null || list.size() == 0) {
			return true;
		} 
		return false;
	}
	
	
	@RequestMapping(value = "/checkEditName", method = RequestMethod.GET)
	public boolean checkEditName(HttpServletRequest request) {
		QzDictionary qzDictionary = qzDictionaryService.getById(request.getParameter("id"));
		String [] names = request.getParameterValues("name");
		String editName = names[1];
		if (qzDictionary != null && editName.equals(qzDictionary.getName())) {
			return true;
		} else {
			List<QzDictionary> list = qzDictionaryService.selectByName(editName);
			if (list == null || list.size() == 0) {
				return true;
			} 
			return false;
		}
	}
	
	
	
	@RequestMapping(value = "/checkEditCode", method = RequestMethod.GET)
	public boolean checkEditCode(HttpServletRequest request) {
		QzDictionary qzDictionary = qzDictionaryService.getById(request.getParameter("id"));
		String [] codes = request.getParameterValues("code");
		String editCode = codes[1];
		if (qzDictionary != null && editCode.equals(qzDictionary.getCode())) {
			return true;
		} else {
			List<QzDictionary> list = qzDictionaryService.selectByCode(editCode);
			if (list == null || list.size() == 0) {
				return true;
			} 
			return false;
		}
	}
	
	
	@RequestMapping(value = "/checkEditDicCode", method = RequestMethod.GET)
	public boolean checkEditDicCode(HttpServletRequest request) {
		QzDictionary qzDictionary = qzDictionaryService.getById(request.getParameter("id"));
		String pid = qzDictionary.getPid();
		String [] codes = request.getParameterValues("code");
		String editCode = codes[1];
		if (qzDictionary != null && editCode.equals(qzDictionary.getCode())) {
			return true;
		} else {
			List<QzDictionary> list = qzDictionaryService.selectByCodeAndPid(editCode, pid);
			if (list == null || list.size() == 0) {
				return true;
			} 
			return false;
		}
	}
	
	
	
	@RequestMapping(value = "/getAll" , method = RequestMethod.GET)
	public Response list (@RequestParam Map<String, Object> paramMap) {
		Response response = new Response();
		response.success(qzDictionaryService.getByMap(paramMap));
		return response;
	}
	
	
	
	@RequestMapping(value = "/getByPage", method = RequestMethod.GET)
	public Response getByPage(@RequestParam Map<String, Object> paramMap) {
		Response response=new Response();
		int pageNum=1;
		int pageSize=10;
		try{
			if(paramMap.containsKey("pageNum")){
				pageNum=Integer.parseInt((String)paramMap.get("pageNum"));
				paramMap.remove("pageNum");
			}
			if(paramMap.containsKey("pageSize")){
				pageSize=Integer.parseInt((String)paramMap.get("pageSize"));
				paramMap.remove("pageSize");
			}
		}catch (Exception e) {
		}
		response.success(qzDictionaryService.getByPage(pageNum, pageSize, paramMap));
		return response;
	}
	
	
	
	@RequestMapping(value = "/get/{id}" ,method = RequestMethod.GET)
	public Response getById(@PathVariable("id") String id) {
		Response response = new Response();
		QzDictionary qzDictionary = qzDictionaryService.getById(id);
		if (qzDictionary != null){ 
			response.success(qzDictionary);
		} else {
			response.failure();
		}
		return response;
	}
	
	
	@RequestMapping(value = "/update",method = RequestMethod.PUT)
	public Response update(@RequestBody @Validated({Default.class,Groups.Modify.class}) QzDictionary qzDictionary){
		Response response = new Response();
		if (qzDictionaryService.update(qzDictionary)) {
			response.success();
		} else {
			response.failure();
		}
		
		return response;
	}
	
	
	@RequestMapping(value = "/upStatus/{id}" ,method = RequestMethod.PUT)
	public Response upStatus(@PathVariable("id") String id) {
		Response response = new Response();
		if (qzDictionaryService.updateStatusById(id)){ 
			response.success();
		} else {
			response.failure();
		}
		return response;
	}
	
	
	
	@RequestMapping(value = "/upStatusByPid/{id}" ,method = RequestMethod.PUT)
	public Response lockByPid(@PathVariable("id") String id) {
		Response response = new Response();
		if (qzDictionaryService.updateStatusByPid(id)){ 
			response.success();
		} else {
			response.failure();
		}
		return response;
	}
	
	
	@RequestMapping(value = "/upStatusById/{id}" ,method = RequestMethod.PUT)
	public Response lockById(@PathVariable("id") String id) {
		Response response = new Response();
		if (qzDictionaryService.lockById(id)){ 
			response.success();
		} else {
			response.failure();
		}
		return response;
	}
	
	
	
	@RequestMapping(value = "/del/{id}",method = RequestMethod.PUT)
	public Response del (@PathVariable("id") String id) {
		Response response = new Response();
		if (qzDictionaryService.delById(id)) {
			response.success();
		} else {
			response.failure();
		}
		return response;
	}
	


}
